/*===========================================================================+
 |   Copyright (c) 2001, 2005 Oracle Corporation, Redwood Shores, CA, USA    |
 |                         All rights reserved.                              |
 +===========================================================================+
 |  HISTORY                                                                  |
 +===========================================================================*/
package xxhci.oracle.apps.xxierp.SalaryAdvance.webui;

import com.sun.java.util.collections.HashMap;

import java.sql.DataTruncation;

import java.text.DateFormat;
import java.text.DecimalFormat;


import java.text.SimpleDateFormat;

import java.util.Calendar;

import oracle.apps.fnd.common.VersionInfo;
import oracle.apps.fnd.framework.OAApplicationModule;
import oracle.apps.fnd.framework.OAException;
import oracle.apps.fnd.framework.OAViewObject;
import oracle.apps.fnd.framework.server.OADBTransaction;
import oracle.apps.fnd.framework.webui.OAControllerImpl;
import oracle.apps.fnd.framework.webui.OAPageContext;
import oracle.apps.fnd.framework.webui.OAWebBeanConstants;
import oracle.apps.fnd.framework.webui.beans.OAWebBean;
import oracle.apps.fnd.framework.webui.beans.message.OAMessageDateFieldBean;
import oracle.apps.fnd.framework.webui.beans.message.OAMessageTextInputBean;

import oracle.jbo.Row;
import oracle.jbo.domain.Date;

/**
 * Controller for ...
 */
public class DateValCO extends OAControllerImpl {
    public static final String RCS_ID = "$Header$";
    public static final boolean RCS_ID_RECORDED = 
        VersionInfo.recordClassVersion(RCS_ID, "%packagename%");

    /**
     * Layout and page setup logic for a region.
     * @param pageContext the current OA page context
     * @param webBean the web bean corresponding to the region
     */
    public void processRequest(OAPageContext pageContext, OAWebBean webBean) {
        super.processRequest(pageContext, webBean);

    }

    /**
     * Procedure to handle form submissions for form elements in
     * a region.
     * @param pageContext the current OA page context
     * @param webBean the web bean corresponding to the region
     */
    public void processFormRequest(OAPageContext pageContext, 
                                   OAWebBean webBean) {
        super.processFormRequest(pageContext, webBean);
        OAApplicationModule am = pageContext.getApplicationModule(webBean);
        am.invokeMethod("getHolidayList");
        OAViewObject holidaysVO = (OAViewObject)am.findViewObject("tempVO1");
        OAMessageDateFieldBean item1 = 
            (OAMessageDateFieldBean)webBean.findChildRecursive("item1");

        OAMessageTextInputBean item2 = 
            (OAMessageTextInputBean)webBean.findChildRecursive("item2");
        OAMessageTextInputBean item3 = 
            (OAMessageTextInputBean)webBean.findChildRecursive("item3");
        OAMessageTextInputBean item4 = 
            (OAMessageTextInputBean)webBean.findChildRecursive("item4");
        OADBTransaction txn = (OADBTransaction)am.getOADBTransaction();
        oracle.jbo.domain.Date now = txn.getCurrentDBDate();
        int stTime=0,endTime=0;
        if(item2.getText()!=null)
            stTime=Integer.parseInt(item2.getValue(pageContext).toString());
        if(item3.getText()!=null)
            endTime=Integer.parseInt(item3.getValue(pageContext).toString()) ;

        java.util.Date fDate = null, cDate = null;
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            if (item1.getText(pageContext) != null)
                fDate = 
                        (java.util.Date)dateFormat.parse(item1.getValue(pageContext).toString());
            cDate = 
                    (java.util.Date)dateFormat.parse(txn.getCurrentDBDate().toString());


            if (pageContext.getParameter("event").equalsIgnoreCase("item1_Validation")) {


                if (fDate.before(cDate)) {
                    item1.setValue(pageContext, "");
                    OAException successMessage = 
                        new OAException("Date Should be Greater Than Current Date.", 
                                        OAException.ERROR);
                    pageContext.putDialogMessage(successMessage);
                } else {
                    Row row;
                    int flag = 0;
                    java.util.Date hdt = null,hday=null;
                    while ((row = holidaysVO.next()) != null) {
                        if (row.getAttribute("Hdate") != null) {
                            hdt = (java.util.Date)dateFormat.parse(row.getAttribute("Hdate").toString());
                           
                            if (hdt.equals(fDate))
                                flag++;
                        }
                    }
                    SimpleDateFormat dayFormat = new SimpleDateFormat("EEE");
                    hday=(java.util.Date)dayFormat.parse(fDate.toString());
                    String str=hday.toString();
                    str=str.substring(0,3);
                    if(flag!=0 ||str.equalsIgnoreCase("Sun")||str.equalsIgnoreCase("Sat"))
                        item4.setValue(pageContext, "2");
                    else
                        item4.setValue(pageContext, "1.5");
                }
            } else if (pageContext.getParameter("event").equalsIgnoreCase("item2_Validation")) {


                if (item1.getText(pageContext) != null) {
                    item2.setValue(pageContext, "");
                    OAException successMessage = 
                        new OAException("Please Enter Date.", 
                                        OAException.ERROR);
                    pageContext.putDialogMessage(successMessage);
                }
//                else if ((item4.getValue(pageContext).equals("1.5")) && (stTime>0800 && stTime<1300) &&(stTime>1400 && stTime<1700) ) {
//                    item2.setValue(pageContext, "");
//                    OAException successMessage =
//                    new OAException("Please Enter Time in Non Working Hrs.",
//                    OAException.ERROR);
//                    pageContext.putDialogMessage(successMessage);
//                } 
                else if (item4.getValue(pageContext).equals("2")) {
                }
            } else if (pageContext.getParameter("event").equalsIgnoreCase("item3_Validation")) {
                if (item1.getText(pageContext) != null) {
                    item3.setValue(pageContext, "");
                    OAException successMessage = 
                        new OAException("Please Enter Date.", 
                                        OAException.ERROR);
                                       
                    pageContext.putDialogMessage(successMessage);

                } 
                    else  if (item2.getText(pageContext) == null) {
                    item3.setValue(pageContext, "");
                    OAException successMessage =
                    new OAException("Please Enter Start Time.",
                    OAException.ERROR);
                    pageContext.putDialogMessage(successMessage);
                }
                else  if (endTime >= stTime)
                {
                item3.setValue(pageContext, "");
                OAException successMessage =
                new OAException("Start Time should be Greater than End Time.",
                OAException.ERROR);
                pageContext.putDialogMessage(successMessage);
                }
//                 else if ((item4.getValue(pageContext).equals("1.5"))  && (endTime>0800 && endTime<1300) &&(endTime>1400 && endTime<1700) ) {
//                    item3.setValue(pageContext, "");
//                    OAException successMessage =
//                    new OAException("Please Enter Time in Non Working Hrs.",
//                    OAException.ERROR);
//                    pageContext.putDialogMessage(successMessage);
//                }
                else if (item4.getValue(pageContext).equals("1.5")) {
                }

            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }

}
